package com.copa.service.impl;

import com.copa.dao.GamesDao;
import com.copa.entity.Games;
import com.copa.service.GamesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author copa
 * @createDate 2022-06-22 22:11
 * @function
 */
@Service
public class GamesServiceImpl implements GamesService {

    @Resource
    private GamesDao gamesDao;

    @Override
    public void insert(Games games) {
        if (hasGameInfo(games.getSaleTime(), games.getChiName())) {
            gamesDao.insert(games);
        }
    }

    @Override
    public List<Games> findBySaleTime(String saleTime) {
        return gamesDao.selectBySaleTime(saleTime);

    }

    private boolean hasGameInfo(String saleTime, String chiName) {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("sale_time", saleTime);
        conditionMap.put("chi_name", chiName);
        List<Games> games = gamesDao.selectByMap(conditionMap);
        return Objects.isNull(games) || games.isEmpty();
    }
}
